﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using IllSoft.Xvcc.Volano.Client;
using System.Diagnostics;
using System.ComponentModel;
using AvalonDock;

namespace IllSoft.Xvcc.WpfUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.mainDockingManager.PropertyChanged += new PropertyChangedEventHandler(mainDockingManager_PropertyChanged);
        }

        void mainDockingManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ActiveContent" && this.mainDockingManager.ActiveContent != null)
            {
                Trace.WriteLine("ActiveContent changed to: " + this.mainDockingManager.ActiveContent.Title);
            }
            else if (e.PropertyName == "ActiveDocument" && this.mainDockingManager.ActiveDocument != null)
            {
                Trace.WriteLine("ActiveDocument changed to: " + this.mainDockingManager.ActiveDocument.Title);
            }
        }

        private void expander1_Collapsed(object sender, RoutedEventArgs e)
        {
            /*DoubleAnimation da = new DoubleAnimation();
            da.To = 35;
            da.Duration = new Duration(TimeSpan.FromSeconds(.25));

            this.expander1.BeginAnimation(Expander.WidthProperty, da);*/
        }

        private void connectButton_Click(object sender, RoutedEventArgs e)
        {
            ServerWindow serverWindow = new ServerWindow();

            this.AddDocument(serverWindow);
        }

        void IChatWindow_Log(object sender, LogEventArgs e)
        {
            this.logRichTextbox.AppendText("[" + DateTime.Now.ToLongTimeString() + "] " + e.Message + Environment.NewLine);
            this.logRichTextbox.ScrollToEnd();
        }

        void IChatWindow_NewDocumentRequired(object sender, NewDocumentEventArgs e)
        {
            this.AddDocument(e.DocumentToAdd);
        }

        private void AddDocument(DocumentContent documentContent)
        {
            IChatWindow chatWindow = documentContent as IChatWindow;

            if (chatWindow != null)
            {
                chatWindow.Log += new EventHandler<LogEventArgs>(IChatWindow_Log);
                chatWindow.NewDocumentRequired += new EventHandler<NewDocumentEventArgs>(IChatWindow_NewDocumentRequired);
            }

            documentContent.Show(this.mainDockingManager);

            documentContent.Focus();
        }
    }
}
